package designpattern.command.llm.command;

import designpattern.command.llm.model.StringBuf;

public class InsertCommand implements Command {
    private final StringBuf stringBuf;
    private final int position;
    private final String text;

    public InsertCommand(StringBuf stringBuf, int position, String text) {
        this.stringBuf = stringBuf;
        this.position = position;
        this.text = text;
    }

    @Override
    public void execute() {
        try {
            stringBuf.insert(position, text);
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public void undo() {
        try {
            stringBuf.delete(position, text.length());
        } catch (IllegalArgumentException e) {
            System.out.println("撤销插入操作失败: " + e.getMessage());
        }
    }
}